<include file='Index/header' />
<div class="row">
	<br>
	<div class="col-sm-12">
		<div class="col-sm-2"></div>
		<div class="col-sm-1" style="padding-top:5px;width:120px">
			<label class="control-label">发布日期：</label>
		</div>
		<div class="col-sm-2">
			<input id="start_date" class="form-control"></input>
		</div>
		<div class="col-sm-1" style="padding-top:5px;width:10px">至</div>
		<div class="col-sm-2">
			<input id="end_date" class="form-control"></input>
		</div>
	</div>

	<div class="col-sm-12">
		<br>
		<div class="col-sm-2">
			<ul id="repository_tree" class="ztree"></ul>
		</div>
		<div class="col-sm-10">
		<table id="secondhand_comment_table" class="table table-hover table-bordered table-striped table-striped">
			<thead>
				<tr>
					<td>A仓库</td>
					<td>B仓库</td>
					<td>二手物品编号</td>
					<td>评论内容</td>
					<td>评论者</td>
					<td>评论时间</td>
					<td>操作</td>
				</tr>
			</thead>
			<tbody>
			</tbody>
		</table>
		</div>
	</div>
	<br>
</div>
<include file='Index/footer' />
<script>
$(document).ready(function() {
		loadRepositoryTree('repository_tree', true);
		loadDatetimepicker('start_date');
		loadDatetimepicker('end_date');
		loadTable();
});
var table;
function loadTable(){
	table = $('#secondhand_comment_table').DataTable({
		"bProcessing": true,
		"bDestroy": true,
		"sPaginationType" : "input",
		"oLanguage" : {
              "sLengthMenu": "每页显示 _MENU_ 条记录",
              "sZeroRecords": "抱歉， 没有找到",
              "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
              "sInfoEmpty": "没有数据",
              "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
              "sZeroRecords": "没有检索到数据",
               "sSearch": "搜索: &nbsp",
              "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
              }
          }, 
		"ajax":'{:U("Admin/Secondhand/getSecondhandComment/")}',
		"columns": [
					{ "data": 'secondhand_product' },
					{ "data": 'secondhand_product' },
					{ "data": 'secondhand_product_id' },
	        { "data": 'content' },
	        { "data": 'from_user' },
	        { "data": 'createtime' },
	        { "data": 'btn' }
    ],
    "columnDefs": [
    {
    	"targets": [0,1],
    	"visible":false
    },
    {
    	"targets": 0,
    	"render": function (data, type, row, table) {
        return data.a_repository_id;
      }
    },
    {
    	"targets": 1,
    	"render": function (data, type, row, table) {
        return data.b_repository_id;
      }
    },
    {
    	"targets": 3,
    	"render": function (data, type, row, table) {
    		var ret = '';
        if(row.to_user){
					ret += '<label>对' + row.to_user.user_id + '说：</label><br>';
        }
        ret += row.content;
        return ret;
      }
    },
    {
    	"targets": 4,
    	"render": function (data, type, row, table) {
        return data.name;
      }
    },
    {
    	"targets": -1,
    	"data": null,
    	"render": function (data, type, row, table) {
    		var btn = '审核通过';
    		var status = '1';
    		if(row.status != '0'){
					btn = '审核不通过';
					status = '0';
    		}
        return '<button class="btn btn-primary" onclick="modStatus(\''+row.secondhand_comment_id+'\', \''+status+'\')">'+btn+'</button>  <button class="btn btn-warning" onclick="del(\''+row.secondhand_comment_id+'\')">删除</button>';
      },
    }]
	});

$.fn.dataTable.ext.search.push(
	    function( settings, data, dataIndex ) {
	        var min = new Date($('#start_date').val());
	        var max = new Date($('#end_date').val());
	        var now = new Date( data[4] ); 

	        if ( ( isNaN( min ) && isNaN( max ) ) ||
	             ( isNaN( min ) && now <= max ) ||
	             ( min <= now   && isNaN( max ) ) ||
	             ( min <= now   && now <= max ) )
	        {
	            return true;
	        }
	        return false;
	    }
	);	

	$('#start_date, #end_date').datetimepicker().on('changeDate', function(e){
      table.draw();
  });
	
}

function modStatus(secondhand_comment_id, status){
	$.ajax({
			url: '{:U("Admin/Secondhand/updateSecondhandComment/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"secondhand_comment_id":secondhand_comment_id,
				"status":status 
			}
		})
		.success(function(data){
			location.reload();
	});
}

function del(secondhand_comment_id){
	if(!confirm("确认删除该评论吗？")){
		return;
	}
	$.ajax({
			url: '{:U("Admin/Secondhand/deleteSecondhandComment/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"secondhand_comment_id":secondhand_comment_id
			}
		})
		.success(function(data){
			location.reload();
	});
}

function searchARepository(val){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(0).search(val).draw();
}

function searchBRepository(val){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(1).search(val).draw();
}

</script>
